<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认证类型" prop="certificationType">
        <el-select v-model="queryParams.certificationType" placeholder="请选择认证类型">
          <el-option
            label="全部类型"
            value="99">
          </el-option>
          <el-option
            label="快递员认证"
            value="0">
          </el-option>
          <el-option
            label="实名认证"
            value="1">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="证件类型" prop="documentType">
        <el-select v-model="queryParams.documentType" placeholder="请选择证件类型">
          <el-option
            label="全部类型"
            value="99">
          </el-option>
          <el-option
            label="工牌/名片"
            value="0">
          </el-option>
          <el-option
            label="身份证"
            value="1">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="time1">
        <el-date-picker
          v-model="queryParams.time1"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核时间" prop="time2">
        <el-date-picker
          v-model="queryParams.time2"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态">
          <el-option
            label="全部类型"
            value="99">
          </el-option>
          <el-option
            label="待审核"
            value="0">
          </el-option>
          <el-option
            label="已通过"
            value="1">
          </el-option>
          <el-option
            label="已拒绝"
            value="2">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="certificationReviewList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id">
        <template slot-scope="scope">
          <span @click="getCertificationReviewById(scope.row.id)" style="color:#6a9eff;text-decoration: underline;">{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="认证类型" align="center" prop="certificationType">
        <template slot-scope="scope">
          {{scope.row.certificationType==0?'快递员认证' :'实名认证'}}
        </template>
      </el-table-column>
      <el-table-column label="证件类型" align="center" prop="documentType">
        <template slot-scope="scope">
          {{scope.row.documentType==0?'工牌/名片':'身份证'}}
        </template>
      </el-table-column>
      <el-table-column label="认证图片" align="center">
        <template slot-scope="scope">
          <img v-if="scope.row.documentType==0" :src="scope.row.workIdPhoto" style="width: 50px;height: 50px">
          <img v-if="scope.row.documentType==1" :src="scope.row.frontOfTheCredential" style="width: 50px;height: 50px">&nbsp;
          <img v-if="scope.row.documentType==1" :src="scope.row.pictureOfHoldingTheFrontOfTheCredential" style="width: 50px;height: 50px">
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="reviewTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          {{scope.row.status==0?'待审核':scope.row.status==1?'已通过':'已拒绝'}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status==0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="approvalCertificationReviewStatus(scope.row.id)"
          >同意</el-button>
          <el-button
            v-if="scope.row.status==0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="openRejectCertificationReview(scope.row.id)"
          >拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改认证审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核拒绝对话框 -->
    <el-dialog title="提示" :visible.sync="openCertificationReview" width="500px" append-to-body>
      <el-form :ref="certificationReview" :model="certificationReview" :rules="rules" label-width="80px">
        您确定要审核拒绝该快递员认证审核？请在下方输入审核不通过的原因
        <el-form-item label="拒绝中文原因" prop="rejectionReasonsCN">
          <el-input v-model="certificationReview.rejectionReasonsCN" placeholder="请输入审核拒绝中文原因" />
        </el-form-item>
        <el-form-item label="拒绝英文原因" prop="rejectionReasonsEN">
          <el-input v-model="certificationReview.rejectionReasonsEN" placeholder="请输入审核拒绝英文原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm(certificationReview)">确 定</el-button>
        <el-button @click='openCertificationReview=false'>取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCertificationReview, getCertificationReview, delCertificationReview, addCertificationReview, updateCertificationReview, approvalCertificationReviewStatus, rejectCertificationReviewStatus } from "@/api/personnel/certificationReview";

export default {
  name: "CertificationReview",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 认证审核表格数据
      certificationReviewList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        certificationType: '99',
        documentType: '99',
        createTime: null,
        reviewTime: null,
        status: '99'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rejectionReasonsCN: [
          { required:true, message: "请输入拒绝中文原因", trigger: "blur"}
        ],
        rejectionReasonsEN: [
          { required:true, message: "请输入拒绝英文原因", trigger: "blur"}
        ]
      },
      openCertificationReview:false,
      certificationReview:{},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询认证审核列表 */
    getList() {
      this.loading = true;
      listCertificationReview(this.queryParams).then(response => {
        this.certificationReviewList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //
    getCertificationReviewById(id){
      this.$router.push({
        path: '/personnel/details-data/index/' + id
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        certificationType: null,
        documentType: null,
        workIdPhoto: null,
        frontOfTheCredential: null,
        pictureOfHoldingTheFrontOfTheCredential: null,
        createTime: null,
        reviewTime: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(this.queryParams.time1!=null){
        const time1=this.queryParams.time1;
        this.queryParams.beginCreateTime=time1[0];
        this.queryParams.endCreateTime=time1[1];
      }else{
        this.queryParams.beginCreateTime=null;
        this.queryParams.endCreateTime=null;
      }
      if(this.queryParams.time2!=null){
        const time2=this.queryParams.time2;
        this.queryParams.beginReviewTime=time2[0];
        this.queryParams.endReviewTime=time2[1];
      }else{
        this.queryParams.beginReviewTime=null;
        this.queryParams.endReviewTime=null;
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 审核同意功能
    approvalCertificationReviewStatus(id){
      this.$confirm('您确定要审核通过该快递员认证审核?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        approvalCertificationReviewStatus(id).then(response=>{
          this.getList();
          this.$message({
            type: 'success',
            message: '该快递员审核通过!'
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消禁用'
        });
      });
    },
    //审核拒绝提示弹窗
    openRejectCertificationReview(id){
      this.openCertificationReview=true;
      getCertificationReview(id).then(response=>{
        this.certificationReview=response.data;
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加认证审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCertificationReview(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改认证审核";
      });
    },
    /** 审核拒绝提交按钮 */
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.certificationReview);
          rejectCertificationReviewStatus(this.certificationReview).then(response => {
            this.$modal.msgSuccess("该快递员的审核拒绝");
            this.openCertificationReview = false;
            this.getList();
          });
        } else {
          this.$message.error("请输入所有审核拒绝的原因");
          return false;
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除认证审核编号为"' + ids + '"的数据项？').then(function() {
        return delCertificationReview(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('personnel/certificationReview/export', {
        ...this.queryParams
      }, `certificationReview_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
